<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form :form='form' slot='detail'>
        <a-row>
          <a-col :span='12'>
            <a-form-item label='项目名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                placeholder='请点击选择项目名称'
                v-decorator="['projectName', validatorRules.projectName]"
                :trigger-change='true'
                org-fields='id,opportunity_name,customer_name,project_name,project_charger'
                dest-fields='bidProjectId,opportunityName,customerName,projectName,projectCharger'
                code='bid_project_select'
                @callback='projectNamePopupCallback'
                :disabled="model.bpmStatus==='3'"
              />
              <a-input v-show='false' v-decorator="['bidProjectId']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='销售经理' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['projectCharger'] " placeholder='可选择项目名称自动带入' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='商机名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['opportunityName']" placeholder='可选择项目名称自动带入' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='客户名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['customerName']" placeholder='可选择项目名称自动带入' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='保证金类别' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['depositType',validatorRules.depositType]"
                                 :trigger-change='true' dictCode='icm_deposit_type' placeholder='请选择保证金类别'
                                 :disabled="model.bpmStatus==='3'" />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['leader',validatorRules.leader]" placeholder='请输入收款人'
                       :disabled="model.bpmStatus==='3'"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款银行' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['bankDeposit',validatorRules.bankDeposit]" placeholder='请输入收款银行'
                       :disabled="model.bpmStatus==='3'"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='收款帐户' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['account',validatorRules.account]" placeholder='请输入收款帐户'
                       :disabled="model.bpmStatus==='3'"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='支付金额' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input-number v-decorator="['deposit', validatorRules.deposit]" placeholder='请输入支付金额'
                              style='width: 100%'
                              :min='0'
                              :formatter='value => `${value}元`'
                              :parser="value => value.replace('元','').replace('万','')"
                              :disabled="model.bpmStatus==='3'"
              />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='招标编号' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['tenderCode',validatorRules.tenderCode]" placeholder='请输入招标编号'
                       :disabled="model.bpmStatus==='3'"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='招标公司' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['tenderCompany',validatorRules.tenderCompany]" placeholder='请输入招标公司'
                       :disabled="model.bpmStatus==='3'"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="model.bpmStatus==='3'">
            <a-form-item label='付款日期' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date
                placeholder='请选择付款日期'
                v-decorator="['issueTime', validatorRules.issueTime]"
                :trigger-change='true'
                style='width: 100%'
                :disabled="model.bpmStatus==='3'&&model.issue==='Y'"
              />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="model.bpmStatus==='3'">
            <a-form-item label='付款凭证' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-upload v-decorator="['archiveFile', validatorRules.archiveFile]" :trigger-change='true'
                        :disabled="model.bpmStatus==='3'&&model.issue==='Y'"></j-upload>
            </a-form-item>
          </a-col>


          <a-col :span='6' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='是否归档' :labelCol='labelCol3' :wrapperCol='wrapperCol3'>
              <j-switch v-decorator="['archived', validatorRules.archived]"
              ></j-switch>
            </a-form-item>
          </a-col>
          <a-col :span='6' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='是否中标' :labelCol='labelCol3' :wrapperCol='wrapperCol3'>
              <j-switch v-decorator="['isBidding', validatorRules.archived]" @change='bidChange'
              ></j-switch>
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='退回日期' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date
                placeholder='请选择退回日期'
                v-decorator="['returnDate', validatorRules.returnDate]"
                :trigger-change='true'
                style='width: 100%'

              />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='退回金额' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['returnMoney',validatorRules.returnMoney]" :min='0' type='number'
                       placeholder='请输入退回金额'
              />
            </a-form-item>
          </a-col>

          <a-col :span='12' v-if="model.bpmStatus==='3'&&model.isBidding==='Y'&&model.issue==='Y'">
            <a-form-item label='中标服务费' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['biddingMoney',validatorRules.biddingMoney]" :min='0' type='number'
                       placeholder='请输入中标服务费'
              />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='投标费用' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['tenderMoney',validatorRules.tenderMoney]" :min='0' type='number'
                       placeholder='请输入投标费用'
              />
            </a-form-item>
          </a-col>

          <a-col :span='12' v-if="model.bpmStatus==='3'&&model.issue==='Y'">
            <a-form-item label='收款凭证' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-upload v-decorator="['issueFile', validatorRules.issueFile]" :trigger-change='true'

              ></j-upload>
            </a-form-item>
          </a-col>

          <a-col :span='24'>
            <a-form-item label='汇款备注' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['remarks']" placeholder='请输入汇款备注'
              />
            </a-form-item>
          </a-col>
          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm' type='primary'>提 交</a-button>
          </a-col>
        </a-row>


      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import moment from 'moment'
export default {
  name: 'BidDepositApplyForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      labelCol3: {
        xs: { span: 12 },
        sm: { span: 12 }
      },
      wrapperCol3: {
        xs: { span: 12 },
        sm: { span: 3 }
      },
      confirmLoading: false,
      validatorRules: {
        archiveFile: {
          rules: [{ required: true, message: '请上传归档文件!' }]
        },
        issueFile: {
          rules: [{ required: true, message: '请上传凭证附件!' }]
        },
        deposit: {
          initialValue: 0,
          rules: [
            { required: true, message: '请输入保证金额!' },
            { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' }
          ]
        },
        projectName: {
          rules: [
            { required: true, message: '请选择项目名称!' }
          ]
        },
        leader: {
          rules: [
            { required: true, message: '请输入收款人!' }
          ]
        },
        tenderCode: {
          rules: [
            { required: true, message: '请输入招标编号!' }
          ]
        },
        tenderCompany: {
          rules: [
            { required: true, message: '请输入招标公司!' }
          ]
        },
        bankDeposit: {
          rules: [
            { required: true, message: '请输入开户行!' }
          ]
        },
        account: {
          rules: [
            { required: true, message: '请输入账户!' }
          ]
        },
        depositType: {
          rules: [{ required: true, message: '请选择保证金类别!' }]
        },
        returnDate: {
          rules: [
            { required: true, message: '请选择退回日期!' }
          ]
        },
        tenderMoney: {
          rules: [
            { required: true, message: '请输入投标费用!' }
          ]
        },
        returnMoney: {
          rules: [
            { required: true, message: '请输入退回金额!' }
          ]
        },
        biddingMoney: {
          rules: [
            { required: true, message: '请输入中标服务费!' }
          ]
        },
        issueTime: {
          rules: [
            { required: true, message: '请选择退回日期!' }
          ]
        }

      },
      url: {
        add: '/bid/bidDepositApply/add',
        edit: '/bid/bidDepositApply/edit',
        queryById: '/bid/bidDepositApply/queryById'
      },
      isPay:false,
      fields: [
        'leader',
        'bankDeposit',
        'account',
        'deposit',
        'bidProjectId',
        'bpmStatus',
        'depositType',
        'remarks',
        'opportunityName',
        'customerName',
        'projectName',
        'depositStatus',
        'projectCharger',
        'tenderCode',
        'tenderCompany',
        'isBidding',
        'archived',
        'returnDate',
        'returnMoney',
        'biddingMoney',
        'archiveFile',
        'tenderMoney',
        'issue',
        'issueTime',
        'issueFile'
      ],
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  mounted() {
  },
  watch: {
    //流程表单的提交的监听事件，用来监听修改或者操作流程的表单
    // 同时需要修改 submitForm方法

    '$store.state.bpm.bpmSubmitStates': {
      immediate: true,
      handler(val) {
        if (val && val.edit && val.edit === '1') {
          this.submitForm().then(res => {
            if (res) {
              this.$store.commit('BPM_SUBMIT_STATES', {edit: '2'})
            }
          })
        }
      },
    }
  },
  methods: {
    moment,
    bidChange(value) {
      this.model.isBidding = value
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      if(this.isPay && !this.model.issueTime){
        this.model.issueTime = moment().format('YYYY-MM-DD')
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      return new Promise(((resolve, reject) => {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                resolve(true)
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          } else {
            this.$store.commit('BPM_SUBMIT_STATES', {})
          }
        })
      }))
    },
    projectNamePopupCallback(row) {
      this.bidProjectId = row.bidProjectId
      this.form.setFieldsValue(pick(row, this.fields))
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    }
  }

}
</script>